'''
This is the MyTopo class.

you can still use 'mn --topo=mytopo' from the command line to build it.

You should already have a topology file.

If you want to run this code on your computer, you may need to modify the parameter that path-to-topofile/filename of open() function.

'''

from mininet.topo import Topo
from mininet.log import setLogLevel
from mininet.link import TCLink


class MyTopo(Topo):
    "Topology."
    
    def __init__(self,topo):
        Topo.__init__(self,)
        
        topofile = topo
        sw = []
        host = []
        setLogLevel('info')
        
        with open(topofile) as fin:
            
            numSw_ = fin.readline()
            numSw_ = numSw_.split()
            numSw= int(numSw_[1])

            #add sw
            for i in range(numSw):
                s = self.addSwitch('s%s'%(i+1))
                sw.append(s)

            numHost_ = fin.readline()
            numHost_ = numHost_.split()
            numHost = int(numHost_[1])

            #add host
            for j in range(numHost):
                h = self.addHost('h%s'%(j+1))
                host.append(h)
            #print len(host)
            #print len(sw)

            fin.readline()

            #add link
            for line in fin.readlines():
                line = line.split()
                #print line[0]
                u = int(line[1])
                v = int(line[2])
                if u < numHost:
                    self.addLink(host[u], sw[v-numHost])
                else:
                    self.addLink(sw[u-numHost], sw[v-numHost], bw=1, delay='0.1ms')

topos = {'mytopo':(lambda: MyTopo("/home/zxg/scandir/testtopo.txt"))}
